package edu.towson.cis.cosc490.jday3.lab3;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.*;

public class ToDoListActivity extends Activity implements OnClickListener, OnLongClickListener {
	
	public Button m_addButton;
	public LinearLayout m_itemListView;
	public ArrayList<ToDoItem> toDoItems = new ArrayList<ToDoItem>();
	

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        initLayout();

        m_addButton.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public void initLayout(){
    	m_addButton = (Button) this.findViewById(R.id.addNew);
    	m_itemListView = (LinearLayout) this.findViewById(R.id.toDoListView);
    }
    
    public void updateLayout(){
    	m_itemListView.removeAllViews();
    	ToDoItem item;
    	for(int i=0; i<toDoItems.size(); i++){
    		item = toDoItems.get(i);
    		TextView newItemTextView = new TextView(this);
    		newItemTextView.setTextColor(getResources().getColor(R.color.white));
    		if(i%2==0){
    			newItemTextView.setBackgroundColor(getResources().getColor(R.color.light));
    		}else{
    			newItemTextView.setBackgroundColor(getResources().getColor(R.color.dark));
    		}
    		newItemTextView.setText(item.getPriority()+": "+item.getToDoItem());
    		newItemTextView.setOnLongClickListener(this);
    		m_itemListView.addView(newItemTextView);
    	}
    }

	public void onClick(View v) {
		Intent i = new Intent(this, NewItemActivity.class);
		startActivityForResult(i,1);
	}
	
	public void onActivityResult(int reqC, int resC, Intent intent){
		
		ToDoItem todo = new ToDoItem(intent.getStringExtra("text"),intent.getIntExtra("priority",0));
		toDoItems.add(todo);
        updateLayout();
	}
	
	public boolean onLongClick(View v){
		TextView tv = (TextView) v;
		String message = tv.getText().toString();
		
		Intent smsIntent = new Intent(Intent.ACTION_VIEW);
		
		smsIntent.putExtra("sms_body", "To-Do List Item: " + message);
		smsIntent.putExtra("address", "");
		smsIntent.setType("vnd.android-dir/mms-sms");
		
		startActivity(smsIntent);
		
		return false;
		
	}
}